import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:journal/services/authentication_api.dart';

class AuthenticationService implements AuthenticationApi {
  // final FirebaseAuth _firebaseAuth = FirebaseAuth.instanceFor(app: Firebase.app('journal'));
  // final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  static FirebaseApp? app;
  late FirebaseAuth _firebaseAuth;
  static String uid = '';

  AuthenticationService() {
    _firebaseAuth =  FirebaseAuth.instanceFor(app: app!);
    print("AuthenticationService init app=$app");
  }

  FirebaseAuth getFirebaseAuth() {
    return _firebaseAuth;
  }

  Future<String> currentUserUid() async {
    // return _firebaseAuth.currentUser?.uid ?? uid;
    return uid;
  }

  Future<void> signOut() async {
    return _firebaseAuth.signOut();
  }

  Future<String> signInWithEmailAndPassword({required String email, required String password}) async {
    var user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
    return user.user?.uid ?? '';
  }

  Future<String> createUserWithEmailAndPassword({required String email, required String password}) async {
    var user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
    return user.user?.uid ?? '';
  }

  Future<void> sendEmailVerification() async {
    var user = _firebaseAuth.currentUser;
    user?.sendEmailVerification();
  }

  Future<bool> isEmailVerified() async {
    return _firebaseAuth.currentUser?.emailVerified ?? false;
  }
}
